#!/usr/bin/env python

import sys
from media_manager_class import MediaManager

def add_image ( liwe, module, id_obj, descr, field_name, pos ):
	"""
	:INPUT:
		- module:		SAFE_DB_STRING
		- id_obj:		SAFE_DB_STRING
		- descr:		SAFE_STRING
		- pos:			SAFE_NUMBER

	:xPERMS:
		- admin
	"""
	
	try:
		my_file = liwe.pyhp.uploads [ field_name ]
	except:
		liwe.err_code = 1
		liwe.err_descr = "Nessun file per upload"
		return

	if not descr:
		descr = my_file [ 'filename' ]

	liwe.pyhp.log.debug ( "---- ADD IMAGE: %s - %s" % ( my_file, my_file [ 'file' ] ) )

	mm = MediaManager ( liwe )
	mm.add_image ( module, id_obj, descr, my_file, pos )
	return { "media_items" : mm.get_items ( module, id_obj ) }

def add_flash ( liwe, module, id_obj, descr, field_name, pos ):
	"""
	:INPUT:
		- module:		SAFE_DB_STRING
		- id_obj:		SAFE_DB_STRING
		- descr:		SAFE_STRING
		- pos:			SAFE_NUMBER

	:xPERMS:
		- admin
	"""
	
	try:
		my_file = liwe.pyhp.uploads [ field_name ]
	except:
		liwe.err_code = 1
		liwe.err_descr = "Nessun file per upload"
		return

	if not descr:
		descr = my_file [ 'filename' ]

	liwe.pyhp.log.debug ( "ADD FLASH: %s - %s" % ( my_file, my_file [ 'file' ] ) )

	mm = MediaManager ( liwe )
	mm.add_flash ( module, id_obj, descr, my_file, pos )
	return { "media_items" : mm.get_items ( module, id_obj ) }

def add_youtube ( liwe, module, id_obj, descr, youtube_url, pos ):
	"""
	:INPUT:
		- module:		SAFE_DB_STRING
		- id_obj:		SAFE_DB_STRING
		- descr:		SAFE_STRING
		- youtube_url:		SAFE_DB_STRING
		- pos:			SAFE_NUMBER

	:xPERMS:
		- admin
	"""
	mm = MediaManager ( liwe )
	mm.add_youtube ( module, id_obj, descr, youtube_url, pos )
	return { "media_items" : mm.get_items ( module, id_obj ) }

def add ( liwe, kind, module, id_obj, descr = "", pos = 0, data = '' ):
	"""
	:INPUT:
		- kind:			SAFE_DB_STRING
		- module:		SAFE_DB_STRING
		- id_obj:		SAFE_DB_STRING
		- descr:		SAFE_STRING
		- pos:			SAFE_NUMBER
		- data:			SAFE_DB_STRING

	:xPERMS:
		- admin
	"""
	#sys.stderr.write ( "MEDIA MANAGER ADD\n" )

	if kind == 'youtube':
		return add_youtube ( liwe, module, id_obj, descr, data, pos )
	else:
		res = None
		for k in liwe.pyhp.uploads.iterkeys ():
			if kind == 'image':
				res = add_image ( liwe, module, id_obj, descr, k, pos )
			elif kind == 'flash':
				res = add_flash ( liwe, module, id_obj, descr, k, pos )
		return res

def rotate ( liwe, id_media, direction ):
	"""
	:INPUT:
		- id_media:		SAFE_NUMBER
		- direction:		Can be "left" or "right" - SAFE_DB_STRING

	:xPERMS:
		- admin
	"""
	mm = MediaManager ( liwe )
	mm.rotate ( id_media, direction )

def edit ( liwe, id_media, descr = '', pos = 0, data = '' ):
	"""
	:INPUT:
		- id_media:		SAFE_DB_STRING
		- descr:		SAFE_DB_STRING
		- pos:			SAFE_NUMBER
		- data:			SAFE_DB_STRING

	:xPERMS:
		- admin
	"""
	mm = MediaManager ( liwe )
	mm.edit ( id_media, descr, pos, data )

def del_media ( liwe, id_media ):
	"""
	:INPUT:
		- id_media:		SAFE_DB_STRING

	:xPERMS:
		- admin
	"""
	mm = MediaManager ( liwe )
	return { "media_items" : mm.del_media ( id_media ) }

def del_items ( liwe, module, id_obj ):
	"""	
	:INPUT:
		- module:		SAFE_DB_STRING
		- id_obj		SAFE_DB_STRING
	:xPERMS:
		- admin
	""" 

	mm = MediaManager ( liwe )
	return { 'media_items' : mm.del_items ( module = module, id_obj = id_obj ) } 


def multi_repos ( liwe, repos ):
	"""
	:INPUT:
		- repos:		SAFE_DB_STRING

	:xPERMS:
		- admin
	"""
	mm = MediaManager ( liwe )

	lst = [ int ( x, 10 ) for x in repos.split ( "|" ) ]
	for id_media, pos in zip ( lst [ 0 : : 2 ], lst [ 1 : : 2 ] ):
		mm.change_pos ( id_media, pos )
